<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Session;
use think\Cookie;


class access extends Login
{
	// public function _initialize()
	// {
	// 	$request = Request::instance();
	// 	$this->error("抱歉！没有操作权限！");

 //    	// (var_dump($request->module(),$request->controller(),$request->action()));
		
	// }
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index($page = 1)
    {
    	// return (var_dump($request->module(),$request->controller(),$request->action()));
        $count = Db::name('access')->count('id');
        $count = ceil($count/10);
        //判断1<=$page<max
        if ($page < 1){
            $page = 1;
        }elseif ($page > $count){
            $page = $count;
        }
        $data = Db::name('access')->order(['id'=>'asc'])->page($page,10)->select();
        // return($data);
        return view('access/index',['CSS_PATH'=>config('admin_css'),
            'JS_PATH'=>config('admin_js'),
            'ICON_PATH'=>config('admin_icon'),
            'IMG_PATH'=>config('admin_img'),
            'data'=>$data,
            'count'=>$count,
            'page'=>$page,
            'admin_name'=>Session::get('admin_name'),
            'admin_id'=>Session::get('admin_id')
        ]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $data = Db::name('access')->select();
        // return dump($data);
        return view('access/add',['CSS_PATH'=>config('admin_css'),
            'JS_PATH'=>config('admin_js'),
            'ICON_PATH'=>config('admin_icon'),
            'IMG_PATH'=>config('admin_img'),
            'data'=>$data
        ]);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
        // return $request->param()['pass'];
        // return $request->param();
        
        $data['access'] = $request->param()['access'];
        $data['module'] = $request->param()['module'];
        // return($data);
        $s = Db::name('access')->insert($data);
        if(!$s){
            return '添加失败';
        }
        return '添加成功';

    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function search($page = 1,Request $request)
    {
        $post = $request->post();
        $count = Db::name('access')->where('access','like','%'.$post['like'].'%')->count('id');
        $count = ceil($count/10);
        //判断1<=$page<max
        if ($page < 1){
            $page = 1;
        }elseif ($page > $count){
            $page = $count;
        }
        $data = Db::name('access')->order(['id'=>'asc'])->where('access','like','%'.$post['like'].'%')->page($page,10)->select();
        return view('access/index',['CSS_PATH'=>config('admin_css'),
            'JS_PATH'=>config('admin_js'),
            'ICON_PATH'=>config('admin_icon'),
            'IMG_PATH'=>config('admin_img'),
            'data'=>$data,
            'count'=>$count,
            'page'=>$page,
            'admin_name'=>Session::get('admin_name'),
            'admin_id'=>Session::get('admin_id')
        ]);
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        // return $id;
        $data = Db::name('access')->where('id',$id)->find();
        // var_dump($arr);
        // return var_dump($data,$access);
        return view('access/edit',['CSS_PATH'=>config('admin_css'),
            'JS_PATH'=>config('admin_js'),
            'ICON_PATH'=>config('admin_icon'),
            'IMG_PATH'=>config('admin_img'),
            'data'=>$data,
        ]);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
        //return $request->param();
        // return(var_dump($request->param(),$id));
        // 处理编辑
        $data['access'] = $request->param()['access'];
        $data['module'] = $request->param()['module'];
        // return($data);
        $s = Db::name('access')->where('id',$id)->update($data);
        $str = $s ? '权限信息更新成功；' : '权限信息未更改；';

        return $str;
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
        return Db::name('access')->where('id',$id)->delete();
    }
}
